package servlet;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author： pang-yun
 * @date： 2021-11-25 17:54
 * 功能目的 ：用于  第一个servlet 程序
 */

public class HelloServlet implements Servlet {
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("servlet开始初始化。。。。。。。-----------------------------------------");
        // 打印 servlet 程序别名
        System.out.println(servletConfig.getServletName());
        // 获取 打印  servlet 的 初始化参数
        System.out.println(servletConfig.getInitParameter("username"));
        // 获取 servletContext 对象
        System.out.println(servletConfig.getServletContext());
    }

    @Override
    public ServletConfig getServletConfig() {
        return null;
    }

    /**
     * service 是专门用来处理请求和响应的
     * @param servletRequest
     * @param servletResponse
     * @throws ServletException
     * @throws IOException
     */
    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("service   begin......................");
        //类型转换  确保有
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        //获取请求方式
        String method = httpServletRequest.getMethod();
        if (method.equals("GET")){
            System.out.println("get请求");
        }
        System.out.println("helloServlet service");
    }

    @Override
    public String getServletInfo() {
        return null;
    }

    @Override
    public void destroy() {

    }
}
